<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  activity_model.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-17.
//  Copyright 2011 lishuzu. All rights reserved.
// 
class Collection_model extends CI_Model
{
		function __construct(){
			parent::__construct();
			$this->userinfo = $this->session->userdata('user');
		}

		function get_Collection_by_wid($wid){
			$collection = array();
			$this->db->where('wid', $wid);
			$this->db->where('uid', $this->userinfo['uid']);
			$query = $this->db->get('collection');
			foreach ($query->result_array() as $row) {
				$collection[] = $row;
			}
			return $collection;
		}

		//得到当前uid所有关注的人
		function get_collections($uid, $num = '', $offset = ''){
			$data = $collections =  array();
			$limit = '';
			if ($uid) {
				if($num){
					$offset = $offset ? $offset : 0;
					$limit = " limit ".$offset.", $num";
				}
				$this->db->where('uid', $uid);
				$data['count'] = $this->db->count_all_results('collection');
				$this->db->where('uid', $uid);
				$sql = "SELECT c.wid, c.collect_id ,w.* FROM ".$this->db->dbprefix('collection')." c LEFT JOIN ".$this->db->dbprefix('works')." w ON c.wid=w.wid Where c.uid='".$uid."' ORDER BY c.dateline DESC $limit";
				$query = $this->db->query($sql);
				foreach ($query->result_array() as $row) {
					$row['thumbimg'] = get_workimg($row['filepath'], 'thumb', 'url');
					$row['url'] = base_url().'work/index/'.$row['wid'];
					$row['key'] = get_workkey($row['wid']);
					$bigavatarfile = FCPATH.'upload/avatar/'.get_avatar($row['uid'], 'big', '');
					if (file_exists($bigavatarfile)) {
						$row['avatar_small'] = base_url().'upload/avatar/'.get_avatar($row['uid'], 'small');
					}else{
						$row['avatar_small'] = base_url().'resources/images/avatar_small.png';						
					}
					$collections[$row['wid']] = $row;
				}
				
				$data['collections'] = $collections;
				
				if ($data) {
					return $data;
				}else{
					return false;
				}
			}
			return false;
		}
		
		//得到喜欢关系
		function get_collections_by_wid($wid, $uid){
			$collection = array();
			if ($wid && $uid) {
				$this->db->where('uid', $uid);
				$this->db->where('wid', $wid);
				$query = $this->db->get('collection');
				foreach ($query->result_array() as $row) {
					$collection[] = $row;
				}
				return $collection;
			}
			return false;
		}
		
		function insert_collection($collection){
			if ($collection) {
				$this->db->insert('collection', $collection);
				return $this->db->insert_id();
			}else{
				return false;
			}
		}
		function delete_collection($wid){
			if ($wid && $this->userinfo['uid']) {
				$this->db->where('uid', $this->userinfo['uid']);
				$this->db->where('wid', $wid);
				$this->db->delete('collection');
				return $this->db->affected_rows();
			}else{
				return false;
			}
		}
}
?>